package com.example.forum.controller;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.BuildReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBuildReplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "楼中楼接口")
@Slf4j
@RestController
@RequestMapping("/buildReply")
public class BuildReplyController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IBuildReplyService buildReplyService;

    @Operation(summary = "创建楼中楼")
    @PostMapping("/create")
    public AppResult createBuildReply(HttpServletRequest request,
                                      @Param("楼中楼id") @RequestParam("replyId") @NonNull Long replyId,
                                      @Param("接收者id") @RequestParam("receiverId") @NonNull Long receiverId,
                                      @Param("楼中楼内容") @RequestParam("content") @NonNull String content){
        // 1. 获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            // 禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 3. 校验楼层是否还有
        ArticleReply replyReply = articleReplyService.selectById(replyId);
        if (replyReply == null || replyReply.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_COMMENT_NOT_EXISTS);
        }
        if (replyReply.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_COMMENT_BANNED);
        }

        // 2.检验文章
        Article article = articleService.selectById(replyReply.getArticleId());
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        BuildReply buildReply = new BuildReply();
        buildReply.setArticleId(replyReply.getArticleId());   // 文章id
        buildReply.setPostUserId(replyReply.getPostUserId());    // 楼主id
        buildReply.setSenderId(user.getId());  // 发送者id
        buildReply.setReceiverId(receiverId);   // 接收者id
        buildReply.setReplyReplyId(replyId);  // 关联主表回复的id
        buildReply.setContent(content);

        buildReplyService.createBuildReply(buildReply);
        return AppResult.success();
    }

    @Operation(summary = "获取列表")
    @GetMapping("/getList")
    public AppResult<List<BuildReply>> getBuildReply(@Param("回复主表id") @RequestParam("ar_id") @NonNull Long ar_id){
        // 1. 校验回复是否存在
        ArticleReply articleReply = articleReplyService.selectById(ar_id);
        if (articleReply == null || articleReply.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_COMMENT_NOT_EXISTS);
        }
        if (articleReply.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_COMMENT_BANNED);
        }
        List<BuildReply> buildReplyList = buildReplyService.selectByBuildId(ar_id);
        return AppResult.success(buildReplyList);
    }
}
